<?php

namespace App\Http\Controllers\Api\Recruit;

use App\Http\Controllers\Controller;
use App\Models\RecruitCategory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;

class CategoryController extends Controller
{
    public function addCategory()
    {

    }

    /**
     * 获取招工分类列表
     * @return JsonResponse
     */
    public function categories(): JsonResponse
    {
        // todo: cms 添加了新的分类，这里要清除缓存重新获取最新的分类
        $data = Cache::remember('recruit_categories', 3600, function () {
            return RecruitCategory::with(['children' => function (HasMany $hasMany) {
                $hasMany->select(['id', 'name', 'full_id_path', 'full_name_path', 'parent_id']);
            }])
                ->where('parent_id', 0)
                ->where('status', 1)
                ->get(['id', 'name'])
                ->toArray();
        });

        return result(0, 'ok', $data);
    }

    /**
     * 获取发布招工表单页优先展示的招工分类数据，页面上显示的是最小粒度的分类数据
     * @return JsonResponse
     */
    public function priorityCategory(): JsonResponse
    {
        $data = Cache::remember('recruit_priority_categories', 3600, function () {
            return RecruitCategory::query()
                ->where('parent_id', '!=', 0)
                ->where('status', 1)
                ->where('priority', 1)
                ->limit(6)
                ->get(['id', 'name', 'full_id_path', 'full_name_path'])
                ->toArray();
        });
        return result(0, 'ok', $data);
    }
}
